using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Eve.API
{
    /// <summary>
    /// Represents detailed information about a skill.
    /// </summary>
    /// <remarks>
    /// <para>The <b>Skill</b> class represents detailed information about a single skill within the Eve universe,
    /// including the attributes which affect the learning rate for the skill, as well as the bonuses that the skill
    /// imparts and the requirements needed to train the skill.</para>
    /// <para>The <see cref="Eve.API.Skill.TypeId">TypeID</see>, and <see cref="Eve.API.Skill.GroupId">GroupId</see> properties can be used to retrieve detailed information
    /// from the <b>Eve.Data</b> class library.</para>
    /// <note type="note">The Eve API only returns information about skills which are currently published.</note>
    /// </remarks>
    [Serializable]
    public class Skill
    {
        private NameId type = new NameId();
        private int groupId;
        private string description;
        private int rank;
        private Dictionary<int, int> requiredSkills = new Dictionary<int, int>();
        private Attribute primaryAttribute;
        private Attribute secondaryAttribute;
        private Dictionary<string, string> bonuses = new Dictionary<string, string>();

        /// <summary>
        /// Gets the name of the skill.
        /// </summary>
        /// <value>
        /// The name of the skill.
        /// </value>
        public string Name
        {
            get { return type.Name; }
        }
        /// <summary>
        /// Gets the identifier for the group in which the skill resides.
        /// </summary>
        /// <value>
        /// The Eve internal identfier for the group in which the skill resides.
        /// </value>
        public int GroupId
        {
            get { return groupId; }
        }
        /// <summary>
        /// Gets the identfier for the skill.
        /// </summary>
        /// <value>
        /// The Eve internal identfier for the skill.
        /// </value>
        public int TypeId
        {
            get { return type.Id; }
        }
        /// <summary>
        /// Gets a text description of the skill.
        /// </summary>
        /// <value>
        /// A text description of the skill.
        /// </value>
        public string Description
        {
            get { return description; }
        }
        /// <summary>
        /// Gets the primary attribute that affects learning.
        /// </summary>
        /// <value>
        /// The primary attribute that affects learning of the skill.
        /// </value>
        public Attribute PrimaryAttribute
        {
            get { return primaryAttribute; }
        }
        /// <summary>
        /// Gets the secondary attribute that affects learning.
        /// </summary>
        /// <value>
        /// The secondary attribute that affects the learning of the skill.
        /// </value>
        public Attribute SecondaryAttribute
        {
            get { return secondaryAttribute; }
        }
        /// <summary>
        /// Gets all of the bonuses that the skill imparts.
        /// </summary>
        /// <value>
        /// A mapping of all of the bonuses that the skill imparts.
        /// </value>
        /// <remarks>
        /// Each entry in the dictionary is a key/value pair, where the key represents the bonus type, and the value represents the value of the bonus for that type.
        /// </remarks>
        public Dictionary<string, string> Bonuses
        {
            get { return bonuses; }
        }
        /// <summary>
        /// Gets all of the skills that are required to train the skill.
        /// </summary>
        /// <value>
        /// A mapping of skill type identifiers to required level.
        /// </value>
        /// <remarks>
        /// Each required skill also has a level which must be trained.  This is represented by the value in the key/value pairs returned.
        /// </remarks>
        public Dictionary<int, int> RequiredSkills
        {
            get { return requiredSkills; }
        }

        internal Skill(XmlElement skill)
        {
            type.Name = skill.Attributes["typeName"].Value;
            groupId = int.Parse(skill.Attributes["groupID"].Value);
            type.Id = int.Parse(skill.Attributes["typeID"].Value);
            description = skill["description"].InnerText;
            rank = int.Parse(skill["rank"].InnerText);
            primaryAttribute = EveAPI.GetAttribute(skill["requiredAttributes"]["primaryAttribute"].InnerText);
            secondaryAttribute = EveAPI.GetAttribute(skill["requiredAttributes"]["secondaryAttribute"].InnerText);
            foreach (XmlElement rowset in skill.GetElementsByTagName("rowset"))
            {
                if (rowset.Attributes["name"].Value == "requiredSkills")
                {
                    foreach (XmlElement row in rowset.ChildNodes)
                        requiredSkills.Add(int.Parse(row.Attributes["typeID"].Value), int.Parse(row.Attributes["skillLevel"].Value));
                }
                else if (rowset.Attributes["name"].Value == "skillBonusCollection")
                {
                    foreach (XmlElement row in rowset.ChildNodes)
                        bonuses.Add(row.Attributes["bonusType"].Value, row.Attributes["bonusValue"].Value);
                }
            }
        }
    }
}
